package com.nttuan.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nttuan.flows.FreeTimeListener;
import com.nttuan.model.entity.FreeTime;
import com.nttuan.model.entity.Task;
import com.nttuan.mycalender.R;
import com.nttuan.util.Support;

import java.util.Date;
import java.util.List;

/**
 * Created by NT Tuan on 10/25/2014.
 */
public class FreeTimeAdapter extends BaseAdapter{

    private Context context;
    private List<FreeTime> list;
    private LayoutInflater mInflate;
    private FreeTimeListener listener;
    Resources res;
    private String startOfDate;
    private String endOfDate;
    public FreeTimeAdapter(Context context, List<FreeTime> list){
        this.context = context;
        res = context.getResources();
        startOfDate = res.getString(R.string.start_new_date);
        endOfDate = res.getString(R.string.end_date);
        this.list = list;
        this.mInflate = LayoutInflater.from(context);
    }
    public void setList(List<FreeTime> list){
        this.list.clear();
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHoler holer;
        if(view == null) {
            holer = new ViewHoler();
            view = mInflate.inflate(R.layout.item_free_time2,null);
            holer.freeTime =(TextView)view.findViewById(R.id.textFreeTime);
            holer.freeDuring =(TextView)view.findViewById(R.id.textFreeDuring);
            holer.previousTask =(TextView)view.findViewById(R.id.textPreviousTask);
            holer.timePrevious =(TextView)view.findViewById(R.id.textPreviousTime);
            holer.nextTask =(TextView)view.findViewById(R.id.textNextTask);
            holer.timeNext =(TextView)view.findViewById(R.id.textNextTime);
            holer.imageView =(ImageView)view.findViewById(R.id.iBtnItemAddNew);
            view.setTag(holer);
        }else
            holer =(ViewHoler) view.getTag();
        FreeTime freeTime = list.get(i);
        Date fromDate = freeTime.getFrom();
        Date toDate = Support.addDateTime(fromDate, freeTime.getDuring());
        String str = context.getString(R.string.free_time_date, Support.displayTime(fromDate),Support.displayTime(toDate));
        holer.freeTime.setText(str);
        holer.freeDuring.setText(Support.displayMinute(context, freeTime.getDuring()));
        holer.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.addNew(list.get(i));
            }
        });

        Task task = freeTime.getPreviousTask();
        if(task.getTaskName().equals(startOfDate)){
            holer.previousTask.setText(R.string.none_events);
            holer.timePrevious.setVisibility(View.GONE);
        }else if(task.getTaskName().equals(endOfDate)){
            holer.previousTask.setText(R.string.none_events);
            holer.timePrevious.setVisibility(View.GONE);
        }else {
            //previous
            Task previous = freeTime.getPreviousTask();
            fromDate = previous.getStart();
            toDate =Support.addDateTime(fromDate, previous.getDuring());
            holer.previousTask.setText(task.getTaskName());
            str = context.getString(R.string.free_time_date, Support.displayTime(fromDate),Support.displayTime(toDate));
            holer.timePrevious.setText(str);
        }
        task = freeTime.getNextTask();
        if(task.getTaskName().equals(startOfDate)){
            holer.nextTask.setText(R.string.none_events);
            holer.timeNext.setVisibility(View.GONE);
        }else if(task.getTaskName().equals(endOfDate)){
            holer.nextTask.setText(R.string.none_events);
            holer.timeNext.setVisibility(View.GONE);
        }else {
            //next
            Task next = freeTime.getNextTask();
            fromDate = next.getStart();
            toDate =Support.addDateTime(fromDate, next.getDuring());
            holer.nextTask.setText(task.getTaskName());
            str = context.getString(R.string.free_time_date, Support.displayTime(fromDate),Support.displayTime(toDate));
            holer.timeNext.setText(str);
        }
        return view;
    }
    public void setListener(FreeTimeListener listener){
        this.listener = listener;
    }
    class ViewHoler{
        TextView freeTime;
        TextView freeDuring;
        TextView previousTask;
        TextView timePrevious;
        TextView nextTask;
        TextView timeNext;
        ImageView imageView;

    }
}
